package com.admin.web;

import com.bayss.core.util.FileUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(String.class)
public class DokoTypeHander extends BaseTypeHandler<String> {

    private String urlName = "pic_en,pic_cn,pic_ko,sku_image_cn,sku_image_en,sku_image_ko,spu_image_cn,spu_image_en,spu_image_ko,tag_pic" +
            ",detail_pic_cn,detail_pic_en,detail_pic_ko,big_pic_cn,big_pic_en,big_pic_ko,little_pic_cn,little_pic_en,little_pic_ko" +
            ",head_portrait_url,station_pic,photo_url";

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter);
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String v = rs.getString(columnName);
        if (urlName.contains(columnName) && StringUtils.isNotBlank(v) && v.startsWith("doko")) {
            return FileUtil.imgUrl(v);
        }
        return v;
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return rs.getString(columnIndex);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return cs.getString(columnIndex);
    }
}
